from django.db import models


# Create your models here.

class User(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=100, unique=True, verbose_name="Username")
    password = models.CharField(max_length=100, verbose_name="Password")
    imgurl = models.CharField(max_length=255, null=True, verbose_name="Avatar URL")
    email = models.CharField(max_length=100, null=True, verbose_name="Email")
    phonenumber = models.CharField(max_length=11, null=True, verbose_name="Phone Number")
    login_date = models.DateField(null=True, verbose_name="Last Login Date")
    status = models.IntegerField(null=True, verbose_name="Account Status (0: active, 1: disabled)")
    create_time = models.DateField(null=True, verbose_name="Created At")
    update_time = models.DateField(null=True, verbose_name="Updated At")
    remark = models.CharField(max_length=500, null=True, verbose_name="Remark")

    class Meta:
        db_table = "sys_user"

class Role(models.Model):
    """角色模型"""
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=True, verbose_name="角色名称")
    description = models.CharField(max_length=200, null=True, blank=True, verbose_name="角色描述")
    permissions = models.JSONField(default=list, verbose_name="权限列表")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = "sys_role"
        verbose_name = "角色"
        verbose_name_plural = "角色"

class UserRole(models.Model):
    """用户角色关联模型"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    role = models.ForeignKey(Role, on_delete=models.CASCADE, verbose_name="角色")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = "sys_user_role"
        verbose_name = "用户角色"
        verbose_name_plural = "用户角色"
        unique_together = ('user', 'role')

class Permission(models.Model):
    """权限模型"""
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, unique=True, verbose_name="权限名称")
    code = models.CharField(max_length=100, unique=True, verbose_name="权限代码")
    description = models.CharField(max_length=200, null=True, blank=True, verbose_name="权限描述")
    module = models.CharField(max_length=50, verbose_name="所属模块")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = "sys_permission"
        verbose_name = "权限"
        verbose_name_plural = "权限"

class Book(models.Model):
    title = models.CharField(max_length=200, verbose_name="Title")
    author = models.CharField(max_length=100, verbose_name="Author")
    description = models.TextField(verbose_name="Description")
    stock = models.PositiveIntegerField(verbose_name="Stock")
    borrowed = models.PositiveIntegerField(default=0, verbose_name="Borrowed Count")
    category = models.CharField(max_length=100, verbose_name="Category", null=True, blank=True)

    class Meta:
        db_table = "book"
        verbose_name = "Book"
        verbose_name_plural = "Books"

class BorrowRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="User")
    book = models.ForeignKey(Book, on_delete=models.CASCADE, verbose_name="Book")
    borrow_time = models.DateTimeField(auto_now_add=True, verbose_name="Borrow Time")
    is_returned = models.BooleanField(default=False, verbose_name="Is Returned")

    class Meta:
        db_table = "borrow_record"
        verbose_name = "Borrow Record"
        verbose_name_plural = "Borrow Records"
